﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
namespace IRDev.Library.ExpandedException.Factory
{
    /// <summary>
    /// Where the ExpandedException is created and manipulated.
    /// </summary>
    public static class ExpandedExceptionFactory
    {
        /// <summary>
        /// Creates ExpandedException
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="parameters"></param>
        /// <param name="className"></param>
        /// <param name="methodName"></param>
        /// <param name="messageID"></param>
        /// <returns>ExpandedException</returns>
        public static IRDev.Library.ExpandedException.Common.ExpandedException CreateAndReturnExpandedException(this Exception ex, List<string> parameters, string className, string methodName, string messageID)
        {
            ExpandedException.Common.ExpandedException ee = new ExpandedException.Common.ExpandedException(ex);
            ee.ClassName = className;
            ee.MethodName = methodName;
            ee.MessageID = messageID;
            ee.ParametersInput = ListOfStringToString(parameters, true);
            return ee;
        }
        /// <summary>
        /// Create an XDocument out of an ExpandedException
        /// </summary>
        /// <param name="ee"></param>
        /// <returns></returns>
        public static XDocument ExpandedExceptionToXDocument(this IRDev.Library.ExpandedException.Common.ExpandedException ee)
        {
            XDocument xx = new XDocument();
            xx.Add(new XElement("ExpandedException"));
            try
            {
                //Add one element to the XDocument for each element of the Exception
                xx.Element("ExpandedException").Add(new XElement("MessageID", ee.MessageID));
                xx.Element("ExpandedException").Add(new XElement("Message", ee.Message));
                xx.Element("ExpandedException").Add(new XElement("Class", ee.ClassName));
                xx.Element("ExpandedException").Add(new XElement("Method", ee.MethodName));
                xx.Element("ExpandedException").Add(new XElement("Parameters", ee.ParametersInput));
                xx.Element("ExpandedException").Add(new XElement("DateTime", ee.TimeOfException));
                xx.Element("ExpandedException").Add(new XElement("Reported", ee.Reported));
                xx.Element("ExpandedException").Add(new XElement("StackTrace", ee.StackTrace));
            }
            catch (Exception ex)
            {
                throw ex.CreateAndReturnExpandedException(ee.ToString(), "IRDev.Library.ExpandedException.Factory.Helper", "ExpandedExceptionToXDocument");
            }
            return xx;
        }

        /// <summary>
        /// Same as standard ListOfStringToString From IRDev.Lib, just needed to redo here to remove circular reference.
        /// </summary>
        /// <param name="ls"></param>
        /// <param name="newLineForEachString"></param>
        /// <returns></returns>
        public static string ListOfStringToString(this List<string> ls, bool newLineForEachString)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string s in ls)
            {
                sb.Append(newLineForEachString ? s + "\n" : s);
            }
            return sb.ToString();
        }
        /// <summary>
        /// Creates ExpandedException
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="parameters"></param>
        /// <param name="className"></param>
        /// <param name="methodName"></param>
        /// <returns>ExpandedException</returns>
        public static IRDev.Library.ExpandedException.Common.ExpandedException CreateAndReturnExpandedException(this Exception ex, List<string> parameters, string className, string methodName)
        {
            return CreateAndReturnExpandedException(ex, parameters, className, methodName, "NA");
        }
        /// <summary>
        /// Creates ExpandedException
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="parameters"></param>
        /// <param name="className"></param>
        /// <param name="methodName"></param>
        /// <returns>ExpandedException</returns>
        public static IRDev.Library.ExpandedException.Common.ExpandedException CreateAndReturnExpandedException(this Exception ex, string parameters, string className, string methodName)
        {
            return CreateAndReturnExpandedException(ex, parameters, className, methodName, "NA");
        }
        /// <summary>
        /// Creates ExpandedException
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="parameters"></param>
        /// <param name="className"></param>
        /// <param name="methodName"></param>
        /// <param name="messageID"></param>
        /// <returns>ExpandedException</returns>
        public static IRDev.Library.ExpandedException.Common.ExpandedException CreateAndReturnExpandedException(this Exception ex, string parameters, string className, string methodName, string messageID)
        {
            List<string> ls = new List<string>();
            ls.Add(parameters);
            return ex.CreateAndReturnExpandedException(ls, className, methodName, "NA");
        }
    }
}
